package com.example.popupwindowlibrary;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.popupwindowlibrary.demo2.PWActivity2;
import com.example.popupwindowlibrary.popup.PopupWindowHelper;


public class PwMainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btnDialog1;
    private Button btnDialog2;
    private Button btnDialog3;
    private Button btnDialog4;
    private Button btnDialog5;
    private Button btnDialog6;
    private Button btnDialog7;
    private Button btnDialog8;
    private Button btnDialog9;
    private Button btnDialog10;
    private Button btnDialog11;
    private Button btnDialog12;
    private PopupWindow mPopupWindow;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_popup_test);

        btnDialog1 = (Button) findViewById(R.id.btn_dialog_1);
        btnDialog2 = (Button) findViewById(R.id.btn_dialog_2);
        btnDialog3 = (Button) findViewById(R.id.btn_dialog_3);
        btnDialog4 = (Button) findViewById(R.id.btn_dialog_4);
        btnDialog5 = (Button) findViewById(R.id.btn_dialog_5);
        btnDialog6 = (Button) findViewById(R.id.btn_dialog_6);
        btnDialog7 = (Button) findViewById(R.id.btn_dialog_7);
        btnDialog8 = (Button) findViewById(R.id.btn_dialog_8);
        btnDialog9 = (Button) findViewById(R.id.btn_dialog_9);
        btnDialog10 = (Button) findViewById(R.id.btn_dialog_10);
        btnDialog11 = (Button) findViewById(R.id.btn_dialog_11);
        btnDialog12 = (Button) findViewById(R.id.btn_dialog_12);
        btnDialog1.setOnClickListener(this);
        btnDialog2.setOnClickListener(this);
        btnDialog3.setOnClickListener(this);
        btnDialog4.setOnClickListener(this);
        btnDialog5.setOnClickListener(this);
        btnDialog6.setOnClickListener(this);
        btnDialog7.setOnClickListener(this);
        btnDialog8.setOnClickListener(this);
        btnDialog9.setOnClickListener(this);
        btnDialog10.setOnClickListener(this);
        btnDialog11.setOnClickListener(this);
        btnDialog12.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.btn_dialog_1) {
            showDialog1();
        } else if (id == R.id.btn_dialog_2) {
            showDialog2();
        } else if (id == R.id.btn_dialog_3) {
            showDialog3();
        } else if (id == R.id.btn_dialog_4) {
            showDialog4();
        } else if (id == R.id.btn_dialog_5) {
            showDialog5();
        } else if (id == R.id.btn_dialog_6) {
            showDialog6();
        } else if (id == R.id.btn_dialog_7) {
            showDialog7();
        } else if (id == R.id.btn_dialog_8) {
            showDialog8();
        } else if (id == R.id.btn_dialog_9) {
            showDialog9();
        } else if (id == R.id.btn_dialog_10) {
            showDialog10();
        } else if (id == R.id.btn_dialog_11) {
            showDialog11();
        } else if (id == R.id.btn_dialog_12) {
            showDialog12();
        }
    }

    private void showDialog12() {

    }

    private void showDialog11() {

    }

    private void showDialog10() {

    }

    private void showDialog9() {

    }

    private void showDialog8() {

    }

    private void showDialog7() {

    }

    private void showDialog6() {

    }

    private void showDialog5() {

    }

    private void showDialog4() {
        startActivity(new Intent(PwMainActivity.this, PWActivity2.class));
    }

    private void showDialog3() {
        startActivity(new Intent(PwMainActivity.this, PwActivity1.class));
    }

    /**
     * 简单封装
     */
    private void showDialog2() {
        View view = LayoutInflater.from(this).inflate(R.layout.popupwindowlayout, null, false);
        TextView tv_take_photo = (TextView) view.findViewById(R.id.tv_take_photo);
        TextView tv_open_album = (TextView) view.findViewById(R.id.tv_open_album);
        TextView tv_cancel = (TextView) view.findViewById(R.id.tv_cancel);
        tv_take_photo.setOnClickListener(mOnClickListener);
        tv_open_album.setOnClickListener(mOnClickListener);
        tv_cancel.setOnClickListener(mOnClickListener);

        final PopupWindowHelper.Builder builder = new PopupWindowHelper.Builder();
        builder.context(this)
                .view(view)
                .anchor(btnDialog10)
                .alpha(0.5f)
                .width(ViewGroup.LayoutParams.WRAP_CONTENT)
                .height(ViewGroup.LayoutParams.WRAP_CONTENT)
                .backgroundDrawable(new ColorDrawable())
                .focusable(true)
                .style(R.style.Popupwindow)
//                .position(PopupWindowHelper.SCREEN_BOTTOM);
                .position(PopupWindowHelper.LEFT);
        final PopupWindowHelper popupWindowHelper = builder.build();
        mPopupWindow = popupWindowHelper.show();
    }

    private void showDialog1() {
        openPopupWindow();
    }

    //=========

    /**
     * 常规用法
     */
    @TargetApi(Build.VERSION_CODES.KITKAT)
    @SuppressWarnings("deprecation")
    @SuppressLint({"InflateParams", "RtlHardcoded"})
    private void openPopupWindow() {
        //防止重复按按钮
        if (mPopupWindow != null && mPopupWindow.isShowing()) {
            return;
        }

        //设置PopupWindow的View
        View view = LayoutInflater.from(this).inflate(R.layout.popupwindowlayout, null, false);
        TextView tv_take_photo = (TextView) view.findViewById(R.id.tv_take_photo);
        TextView tv_open_album = (TextView) view.findViewById(R.id.tv_open_album);
        TextView tv_cancel = (TextView) view.findViewById(R.id.tv_cancel);
        tv_take_photo.setOnClickListener(mOnClickListener);
        tv_open_album.setOnClickListener(mOnClickListener);
        tv_cancel.setOnClickListener(mOnClickListener);

        mPopupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        // 设置PopupWindow的宽度
        Display display = getWindowManager().getDefaultDisplay();
//        mPopupWindow.setWidth(display.getWidth() * 9 / 10);
        mPopupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        mPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //        mPopupWindow = new PopupWindow(view, display.getWidth() * 9 / 10, ViewGroup.LayoutParams.WRAP_CONTENT);

        //设置点击弹窗外隐藏自身
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        //点击空白区域PopupWindow消失，这里必须先设置setBackgroundDrawable，否则点击无反应
        // mPopupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
        // mPopupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));
        mPopupWindow.setFocusable(true);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setTouchable(true);

        //设置动画
        mPopupWindow.setAnimationStyle(R.style.Popupwindow);

        //设置位置
//        mPopupWindow.showAtLocation(btnDialog11, Gravity.CENTER, 0, 0);//屏幕居中
//        mPopupWindow.showAtLocation(btnDialog11, Gravity.CENTER | Gravity.BOTTOM, 0, 0);//屏幕底下居中
        mPopupWindow.showAsDropDown(btnDialog11, 0, 0);//在 anchor 的下边显示

        //设置背景色
        setBackgroundAlpha(0.5f);

        //设置消失监听
        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setBackgroundAlpha(1.0f);
            }
        });
    }


    //设置屏幕背景透明效果
    public void setBackgroundAlpha(float alpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = alpha;
        getWindow().setAttributes(lp);
    }

    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int id = v.getId();
            if (id == R.id.tv_take_photo) {
                if (mPopupWindow != null && mPopupWindow.isShowing()) {
                    toast("我要拍照。。。");
                    mPopupWindow.dismiss();
                }
            } else if (id == R.id.tv_open_album) {
                if (mPopupWindow != null && mPopupWindow.isShowing()) {
                    toast("打开相册！！！");
                    mPopupWindow.dismiss();
                }
            } else if (id == R.id.tv_cancel) {
                if (mPopupWindow != null && mPopupWindow.isShowing()) {
                    mPopupWindow.dismiss();
                }
            }
        }
    };

    private void toast(String message) {
        Toast.makeText(PwMainActivity.this, message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onBackPressed() {
        //避免内存泄漏
        if (mPopupWindow != null && mPopupWindow.isShowing()) {
            mPopupWindow.dismiss();
        } else {
            super.onBackPressed();
        }
    }
}
//=========
